package com.huilai.platform.country_platform.controller;

import com.huilai.platform.country_platform.core.Msg;
import com.huilai.platform.country_platform.core.Result;
import com.huilai.platform.country_platform.dto.ChatDataVO;
import com.huilai.platform.country_platform.dto.RecruitmentDTO;
import com.huilai.platform.country_platform.model.*;
import com.huilai.platform.country_platform.service.AttractionsService;
import com.huilai.platform.country_platform.service.ChatService;
import com.huilai.platform.country_platform.service.InformationService;
import com.huilai.platform.country_platform.service.UserService;
import com.huilai.platform.country_platform.util.CookieUtil;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 作者：洪霓
 * 日期：2023/2/18
 * 注释：
 */

@Controller
@RequestMapping("/information")
public class InformationController {
    @Autowired
    private InformationService informationService;

    @Autowired
    private AttractionsService attractionsService;

    @Autowired
    private UserService userService;

    @Autowired
    private ChatService chatService;

    @Value("${file.upload.path}")
    private String filePath;

    @Value("${file.upload.relative}")
    private String fileRelativePath;

    @RequestMapping("/recruitmentListUI")
    public String recruitmentListUI(Model model, @ModelAttribute("searchName") String searchName, @PageableDefault(size = 10) Pageable pageable){
//        招工就业页面
//        Page<InformationRecruitment> page = informationService.recruitmentListUI(searchName, pageable);
        Page<RecruitmentDTO> page = informationService.recruitmentListUI2(searchName, pageable);

        List<AttractionsBuilding> top10Building = attractionsService.getTopBuilding();
        List<AttractionsWaterscape> top10Waterscape = attractionsService.getTopWaterscape();
        model.addAttribute("top10Building", top10Building);
        model.addAttribute("top10Waterscape", top10Waterscape);
        model.addAttribute("page", page);
        return "information/information-recruitment";
    }

    @RequestMapping("/recruitmentDetailsUI")
    public String recruitmentDetailsUI(HttpServletRequest request, Model model, @ModelAttribute("id")Integer id, @PageableDefault(size = 10)Pageable pageable){
//        招工就业详细页面
        Cookie cookie = CookieUtil.get(request, "username");
        if (cookie == null) {
            throw new ServiceException("用户未登录");
        }
        User user = userService.getUserByUsername(cookie.getValue());
//        Integer fromId = user.getId();
//        UserRecruitment userRecruitment = informationService.findUserRecruitmentByRecruitmentId(id);
//        String toName = null;
//        Integer linkId = 0;
//        if (userRecruitment!=null){
//            Integer toId = userRecruitment.getUserId();
//            chatService.isFirstChat(fromId, toId);
//            toName = userService.getUserById(toId).getUsername();
//            ChatLink chatLink = chatService.findChatLink(fromId, toId);
//            if (chatLink != null){
//                linkId = chatLink.getId();
//            }
//        }
        Boolean flag = informationService.isPushUser(id, request);
        InformationRecruitment informationRecruitment = informationService.findRecruitmentById(id);
        List<AttractionsWaterscape> top10Waterscape = attractionsService.getTopWaterscape();
        List<AttractionsBuilding> top10Building = attractionsService.getTopBuilding();
//        model.addAttribute("linkId",linkId);
//        model.addAttribute("toName",toName);
        model.addAttribute("informationRecruitment",informationRecruitment);
        model.addAttribute("flag", flag);
        model.addAttribute("top10Building",top10Building);
        model.addAttribute("top10Waterscape",top10Waterscape);
        return "information/information-recruitment-details";
    }

    @RequestMapping("/pushRecruitmentListUI")
    public String pushRecruitmentListUI(Model model, HttpServletRequest request,@PageableDefault(size = 10)Pageable pageable){
        Page<RecruitmentDTO> page = informationService.pushRecruitmentListUI(request, pageable);
        model.addAttribute("page",page);
        return "information/information-pushRecruitment";
    }

    @RequestMapping("/mypush")
    public String mypush(Model model, HttpServletRequest request,
                         @PageableDefault(size = 10, sort = { "id" }, direction = Sort.Direction.ASC)Pageable pageable){
        Page<RecruitmentDTO> page = informationService.pushRecruitmentListUI(request, pageable);
        model.addAttribute("page",page);
        return "information/mypush";
    }


    @PostMapping("/addRecruitment")
    @ResponseBody
    public Msg addRecruitment(HttpServletRequest request, InformationRecruitment informationRecruitment){
        Integer result = informationService.addRecruitment(request, informationRecruitment);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();
    }
    @GetMapping("/getRecruitmentById")
    @ResponseBody
    public Msg getRecruitmentById(@RequestParam("id")Integer id){
        // 根据id获得招工就业信息
        InformationRecruitment informationRecruitment = informationService.findRecruitmentById(id);
        return Msg.success().add("informationRecruitment",informationRecruitment);
    }
    @PutMapping("/updateRecruitment")
    @ResponseBody
    public  Msg updateRecruitment(@RequestParam("id")Integer id, InformationRecruitment informationRecruitment){
//        更新招工就业信息
        informationRecruitment.setId(id);
        Integer result = informationService.updateRecruitment(informationRecruitment);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @GetMapping("/getInformationDescribeById")
    @ResponseBody
    public Msg getInformationDescribeById(@RequestParam("id")Integer id){
        // 根据id获得招工就业信息
        UserRecruitment userRecruitment = informationService.findUserRecruitmentByRecruitmentId(id);
        return Msg.success().add("userRecruitment",userRecruitment);
    }


    @RequestMapping("/uploadFile")
    @ResponseBody
    public Msg uploadFile(@RequestParam("file") MultipartFile multipartFile){
        System.out.println(multipartFile.getOriginalFilename());
        String fileName = multipartFile.getOriginalFilename();
        String filePrefixName = fileName.substring(0, fileName.indexOf("."));
        System.out.println(filePrefixName);
        String newFilePath = filePath;
        File file = new File(newFilePath, fileName);
        if (!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        try{
            multipartFile.transferTo(new File(newFilePath+File.separator+fileName));
            return Msg.success();
        }catch (IOException e){
            e.printStackTrace();
        }
        return Msg.fail();
    }

    @GetMapping("/getData")
    @ResponseBody
    public Msg getData(HttpServletRequest request, @RequestParam("id")Integer id){
        Cookie cookie = CookieUtil.get(request, "username");
        if (cookie == null) {
            throw new ServiceException("用户未登录");
        }
        User user = userService.getUserByUsername(cookie.getValue());
        Integer fromId = user.getId();
        UserRecruitment userRecruitment = informationService.findUserRecruitmentByRecruitmentId(id);
        String toName = null;
        Integer linkId = 0;
        if (userRecruitment!=null){
            Integer toId = userRecruitment.getUserId();
            chatService.isFirstChat(fromId, toId);
            toName = userService.getUserById(toId).getUsername();
            ChatLink chatLink = chatService.findChatLink(fromId, toId);
            if (chatLink != null){
                linkId = chatLink.getId();
            }
        }
        return Msg.success().add("chatData",new ChatDataVO(linkId, toName));
    }

    @PutMapping("/updateWindow")
    @ResponseBody
    public  Msg updateWindow(@RequestParam("linkId")Integer linkId, @RequestParam("id")Integer id){
        Integer integer = chatService.updateWindow(linkId, id, 1);
        if (integer == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    @PutMapping("/closeWindow")
    @ResponseBody
    public  Msg closeWindow(@RequestParam("linkId")Integer linkId, @RequestParam("id")Integer id){
        Integer integer = chatService.updateWindow(linkId, id, 0);
        if (integer == 1){
            return Msg.success();
        }
        return Msg.fail();
    }


}
